package com.southwind.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.southwind.entity.Employee;
import com.southwind.entity.LoginRecord;
import com.southwind.service.EmployeeService;
import com.southwind.service.LoginRecordService;
import com.southwind.util.AesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.Random;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private LoginRecordService loginRecordService;



    @PostMapping("/verify")
    public String verify(String phone, String password, String code, Model model,HttpSession session,HttpServletRequest request){
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        Employee one = this.employeeService.getOne(queryWrapper);
        if (one == null) {
            model.addAttribute("errorMsg", "手机号不存在");
            return "login";
        } else {
            password = AesUtils.encryptStr(password,AesUtils.SECRETKEY); // 对传输过来的password密码进行加密
            if (!one.getPassword().equals(password)) {
                model.addAttribute("errorMsg","密码错误");
                return "login";
            } else {
                String code1 = (String) session.getAttribute("code");
                if (!code1.equals(code)) {
                    model.addAttribute("errorMsg","验证码错误");
                    return "login";
                } else {
                    session.setAttribute("employee", one);
                    // 登录成功后，将登录记录保存到数据库
                    LoginRecord loginRecord = new LoginRecord();
                    loginRecord.setEmployeeId(one.getEmployeeId());
                    loginRecord.setEmployeeName(one.getEmployeeName());
                    loginRecord.setTime(LocalDateTime.now());
                    loginRecord.setIpaddress(request.getRemoteAddr());
                    boolean save = this.loginRecordService.save(loginRecord);
                    if (save) return "redirect:/index";
                    return "login";
                }
            }
        }
    }

    @PostMapping("/verifyPassword")
    @ResponseBody
    public String verifyPassword(String password, HttpSession session) {
        Employee employee = (Employee)session.getAttribute("employee");
        password = AesUtils.encryptStr(password,AesUtils.SECRETKEY);
        if (employee.getPassword().equals(password)) return "success";
        return "fail";
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public String updatePassword(String password, HttpSession session) {
        Employee employee = (Employee)session.getAttribute("employee");
        password = AesUtils.encryptStr(password,AesUtils.SECRETKEY);
        employee.setPassword(password);
        boolean updateById = this.employeeService.updateById(employee);
        if (updateById) return "success";
        return "fail";
    }

    @GetMapping("/exit")
    public String exit(HttpSession session){
        session.invalidate();
        return "redirect:/login";
    }

    @RequestMapping("/enimg")
    public void enimg(HttpServletResponse response, HttpSession session){
        try {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            int width = 110, height = 20;
            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            OutputStream os = response.getOutputStream();
            Graphics g = image.getGraphics();
            Random random = new Random();
            g.setColor(getRandColor(200, 250));
            g.fillRect(0, 0, width, height);

            g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
            g.setColor(getRandColor(160, 200));
            for (int i = 0; i < 155; i++) {
                int x = random.nextInt(width);
                int y = random.nextInt(height);
                int xl = random.nextInt(12);
                int yl = random.nextInt(12);
                g.drawLine(x, y, x + xl, y + yl);
            }
            String sRand = "";
            for (int i = 0; i < 4; i++) {
                String rand = "";
                rand = String.valueOf(random.nextInt(10));
                sRand += rand;
                g.setColor(new Color(20 + random.nextInt(10), 20 + random.nextInt(110), 20 + random.nextInt(110)));
                g.drawString(rand, 17 * i + 6, 16);
            }
            session.setAttribute("code", sRand);
            g.dispose();

            //设置为内存进行图片缓存
            ImageIO.setUseCache(false);
            ImageIO.write(image, "JPEG", os);
            os.flush();
            os.close();
            os = null;
            response.flushBuffer();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255)
            fc = 255;
        if (bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }
}
